package com.ccrfid.rmc6.resource.business.rollcall;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.business.rollcall2.*;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.business.rollcall2.Rollcall2ConfigService;
import com.ccrfid.rmc6.service.business.rollcall2.Rollcall2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.List;

@Secured
@Path("rc")
@Produces(MediaType.APPLICATION_JSON)
public class Rollcall2Resource {
    @Autowired
    private Rollcall2ConfigService rollcall2ConfigService;
    @Autowired
    private Rollcall2Service rollcall2Service;

    //============================================
    //1.1 分页获取点名对象组信息
    @GET
    @Path("org-groups")
    public Page<RcOrgGroupEntity> getRcOrgGroupPage(@QueryParam("groupName") String groupName,
                                                    @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (!StringUtils.isEmpty(groupName)) {
            criteria.add(new Criterion("groupName", Criterion.Operator.LIKE, '%' + groupName + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcall2ConfigService.getRcOrgGroupPage(criteria, pageRequest);
    }

    //1.2 获取单个点名对象组信息
    @GET
    @Path("org-groups/{groupId}")
    public RcOrgGroupEntity getRcOrgGroupById(@PathParam("groupId") Long groupId) throws AppException {
        return rollcall2ConfigService.getRcOrgGroupById(groupId);
    }

    //1.3 新增点名对象组
    @POST
    @Path("org-groups")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcOrgGroupEntity createRcOrgGroup(RcOrgGroupEntity entity) throws AppException {
        return rollcall2ConfigService.createRcOrgGroup(entity);
    }

    //1.4 修改点名对象组信息
    @PUT
    @Path("org-groups/{groupId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcOrgGroupEntity updateRcOrgGroup(@PathParam("groupId") Long groupId,
                                             RcOrgGroupEntity entity) throws AppException {
        return rollcall2ConfigService.updateRcOrgGroup(groupId, entity);
    }

    //1.5 删除某个点名对象组
    @DELETE
    @Path("org-groups/{groupId}")
    public Boolean deleteRcOrgGroup(@PathParam("groupId") Long groupId) {
        return rollcall2ConfigService.deleteRcOrgGroup(groupId);
    }

    //1.6 获取某个点名对象组全部成员
    @GET
    @Path("org-groups/{groupId}/members")
    public List<RcOrgGroupMemberEntity> getRcOrgGroupMembers(@PathParam("groupId") Long groupId) throws AppException {
        return rollcall2ConfigService.getRcOrgGroupMembers(groupId);
    }

    //1.7 为点名对象组添加成员
    @POST
    @Path("org-groups/{groupId}/members")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcOrgGroupMemberEntity createRcOrgGroupMember(@PathParam("groupId") Long groupId,
                                                         RcOrgGroupMemberEntity entity) throws AppException {
        return rollcall2ConfigService.createRcOrgGroupMember(groupId, entity);
    }

    //1.8 为点名对象组删除成员
    @DELETE
    @Path("org-groups/{groupId}/members/{memberId}")
    public Boolean deleteRcOrgGroupMember(@PathParam("groupId") Long groupId,
                                          @PathParam("memberId") Long memberId) throws AppException {
        return rollcall2ConfigService.deleteRcOrgGroupMember(groupId, memberId);
    }

    //====================================================
    //2.1 分页获取区域组信息
    @GET
    @Path("location-groups")
    public Page<RcLocationGroupEntity> getRcLocationGroupPage(@QueryParam("groupName") String groupName,
                                                              @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (!StringUtils.isEmpty(groupName)) {
            criteria.add(new Criterion("groupName", Criterion.Operator.LIKE, '%' + groupName + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcall2ConfigService.getRcLocationGroupPage(criteria, pageRequest);
    }

    //2.2 获取单个区域组信息
    @GET
    @Path("location-groups/{groupId}")
    public RcLocationGroupEntity getRcLocationGroupById(@PathParam("groupId") Long groupId) throws AppException {
        return rollcall2ConfigService.getRcLocationGroupById(groupId);
    }

    //2.3 新增区域组
    @POST
    @Path("location-groups")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcLocationGroupEntity createRcLocationGroup(RcLocationGroupEntity entity) throws AppException {
        return rollcall2ConfigService.createRcLocationGroup(entity);
    }

    //2.4 修改区域组信息
    @PUT
    @Path("location-groups/{groupId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcLocationGroupEntity updateRcLocationGroup(@PathParam("groupId") Long groupId,
                                                       RcLocationGroupEntity entity) throws AppException {
        return rollcall2ConfigService.updateRcLocationGroup(groupId, entity);
    }

    //2.5 删除某个区域组
    @DELETE
    @Path("location-groups/{groupId}")
    public Boolean deleteRcLocationGroup(@PathParam("groupId") Long groupId) throws AppException {
        return rollcall2ConfigService.deleteRcLocationGroup(groupId);
    }

    //2.6 获取某个区域组全部成员
    @GET
    @Path("location-groups/{groupId}/members")
    public List<RcLocationGroupMemberEntity> getRcLocationGroupMembers(@PathParam("groupId") Long groupId) throws AppException {
        return rollcall2ConfigService.getRcLocationGroupMembers(groupId);
    }

    //2.7 为区域组添加成员
    @POST
    @Path("location-groups/{groupId}/members")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcLocationGroupMemberEntity createRcLocationGroupMember(@PathParam("groupId") Long groupId,
                                                                   RcLocationGroupMemberEntity entity) throws AppException {
        return rollcall2ConfigService.createRcLocationGroupMember(groupId, entity);
    }

    //2.8 为区域组删除成员
    @DELETE
    @Path("location-groups/{groupId}/members/{memberId}")
    public Boolean deleteRcLocationGroupMember(@PathParam("groupId") Long groupId,
                                               @PathParam("memberId") Long memberId) throws AppException {
        return rollcall2ConfigService.deleteRcLocationGroupMember(groupId, memberId);
    }

    //==========================================
    //3.1 分页查询点名计划信息
    @GET
    @Path("schedules")
    public Page<RcScheduleEntity> getRcSchedulePage(@QueryParam("enabled") Integer enabled,
                                                    @QueryParam("remark") String remark,
                                                    @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        if (!StringUtils.isEmpty(remark)) {
            criteria.add(new Criterion("remark", Criterion.Operator.LIKE, '%' + remark + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcall2ConfigService.getRcSchedulePage(criteria, pageRequest);
    }

    //3.2 获取某个点名计划信息
    @GET
    @Path("schedules/{scheduleId}")
    public RcScheduleEntity getRcScheduleById(@PathParam("scheduleId") Long scheduleId) throws AppException {
        return rollcall2ConfigService.getRcScheduleById(scheduleId);
    }

    //3.3 新增一个点名计划
    @POST
    @Path("schedules")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcScheduleEntity createRcSchedule(RcScheduleEntity entity) throws AppException {
        return rollcall2ConfigService.createRcSchedule(entity);
    }

    //3.4 修改某个点名计划
    @PUT
    @Path("schedules/{scheduleId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcScheduleEntity updateRcSchedule(@PathParam("scheduleId") Long scheduleId, RcScheduleEntity entity)
            throws AppException {
        return rollcall2ConfigService.updateRcSchedule(scheduleId, entity);
    }

    //3.5 删除某个点名计划
    @DELETE
    @Path("schedules/{scheduleId}")
    public Boolean deleteRcScheduleById(@PathParam("scheduleId") Long scheduleId) throws AppException {
        return rollcall2ConfigService.deleteRcScheduleById(scheduleId);
    }

    //==========================================
    //4.1 分页查询点名记录
    @GET
    @Path("records")
    public Page<RcRecordEntity> getRcRecordPage(@QueryParam("startTime") Timestamp startTime,
                                                @QueryParam("stopTime") Timestamp stopTime,
                                                @QueryParam("status") Integer status,
                                                @QueryParam("submitStatus") Integer submitStatus,
                                                @QueryParam("orgGroupId") Long orgGroupId,
                                                @QueryParam("orgGroupName") String orgGroupName,
                                                @QueryParam("locationGroupId") Long locationGroupId,
                                                @QueryParam("locationGroupName") String locationGroupName,
                                                @QueryParam("orgId") Long orgId,
                                                @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (startTime != null) {
            criteria.add(new Criterion("rollcallTime", Criterion.Operator.GTE, startTime));
        }
        if (stopTime != null) {
            criteria.add(new Criterion("rollcallTime", Criterion.Operator.LTE, stopTime));
        }
        if (status != null) {
            criteria.add(new Criterion("status", Criterion.Operator.EQ, status));
        }
        if (submitStatus != null) {
            criteria.add(new Criterion("submitStatus", Criterion.Operator.EQ, submitStatus));
        }
        if (orgGroupId != null) {
            criteria.add(new Criterion("orgGroupId", Criterion.Operator.EQ, orgGroupId));
        }
        if (!StringUtils.isEmpty(orgGroupName)) {
            criteria.add(new Criterion("orgGroupName", Criterion.Operator.LIKE, '%' + orgGroupName + '%'));
        }
        if (locationGroupId != null) {
            criteria.add(new Criterion("locationGroupId", Criterion.Operator.EQ, locationGroupId));
        }
        if (!StringUtils.isEmpty(locationGroupName)) {
            criteria.add(new Criterion("locationGroupName", Criterion.Operator.LIKE, '%' + locationGroupName + '%'));
        }
        if(orgId != null){
            List<Long> groupIdList = rollcall2Service.getGroupIdByOrgId(orgId);
            if(groupIdList.size()>0){
                criteria.add(new Criterion("orgGroupId", Criterion.Operator.IN, groupIdList));
            }else{
                criteria.add(new Criterion("orgGroupId", Criterion.Operator.EQ,0));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcall2Service.getRcRecordPage(criteria, pageRequest);
    }

    //4.2 临时开始一个点名
    @POST
    @Path("{orgGroupId}/{locationGroupId}/roll-call")
    public RcRecordEntity doRollCall(@PathParam("orgGroupId") Long orgGroupId,
                                     @PathParam("locationGroupId") Long locationGroupId) throws AppException {
        return rollcall2Service.doRollCall(orgGroupId, locationGroupId, 0);
    }

    //4.3 提交临时点名结果
    @PUT
    @Path("records/{recordId}/submission")
    public RcRecordEntity submitRcRecord(@PathParam("recordId") Long recordId) throws AppException {
        return rollcall2Service.submitRcRecord(recordId);
    }

    //4.4 分页查询某个点名记录详细结果
    @GET
    @Path("records/{recordId}/details")
    public Page<RcRecordDetailEntity> getRcRecordDetailPage(@PathParam("recordId") Long recordId,
                                                            @QueryParam("personnelName") String personnelName,
                                                            @QueryParam("cardNumber") Long cardNumber,
                                                            @QueryParam("rollcallStatus") List<Integer> statusList,
                                                            @QueryParam("remark") String remark,
                                                            @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (recordId != null) {
            criteria.add(new Criterion("recordId", Criterion.Operator.EQ, recordId));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, "%" + personnelName + "%"));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (statusList != null && statusList.size() > 0) {
            criteria.add(new Criterion("rollcallStatus", Criterion.Operator.IN, statusList));
        }
        if (!StringUtils.isEmpty(remark)) {
            criteria.add(new Criterion("remark", Criterion.Operator.LIKE, "%" + remark + "%"));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcall2Service.getRcRecordDetailPage(criteria, pageRequest);
    }

    //4.5 手工点到某个人/修改点到状态
    @PUT
    @Path("record-detail/{detailId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public RcRecordDetailEntity updateRcRecordDetail(@PathParam("detailId") Long detailId,
                                                     RcRecordDetailEntity entity) throws AppException {
        return rollcall2Service.updateRcRecordDetail(detailId, entity);
    }
}
